<template>
  <main>
    <div class="board">
      <div ref="pieces" class="piece-container">
        <div @click="putPiece(index)" :data-index = index @mouseenter="mouseEnter(index)" @mouseleave="mouseLeave(index)" v-for="(item,index) in grids"></div>
      </div>
      <div></div>
    </div>
    <div class="otherPanel">
      <div>
        <div class="opponentInformation">
          <span>你的对手：</span>
          <span>机器人</span>
        </div>
        <div class="firstInformation">
          <span>你是：</span>
          <span>{{firstShow}}</span>
        </div>
        <div class="pieceInformation">
          <span>你使用的：</span>
          <span>{{statusShow}}</span>
        </div>
        <div class="setContainer">
          <el-switch
              v-model="status"
              :disabled="GameData.isGaming"
              size="large"
              inline-prompt
              style="--el-switch-on-color: #303133; --el-switch-off-color: #DCDFE6"
              active-text="黑子"
              inactive-text="白子"
          />
          <el-switch
              v-model="first"
              :disabled="GameData.isGaming"
              size="large"
              inline-prompt
              style="--el-switch-on-color: #f89898; --el-switch-off-color: #79bbff"
              active-text="后手"
              inactive-text="先手"
          />
        </div>
        <div class="start">
          <el-button :disabled="GameData.isGaming" @click="startGaming(true)" round>开始</el-button>
          <el-button :disabled="!GameData.isGaming" @click="toLose" round>认输</el-button>
          <el-button @click="toBack" round>返回</el-button>
        </div>
      </div>
      <div :class="{Show:GameData.gameType===1}">
        <span>在线房间</span>
      </div>
    </div>
  </main>
</template>

<script setup>
import {computed, onBeforeUnmount, onMounted, reactive, ref, watch} from "vue";
import {useGameDataStore} from "@/stores/game.js";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";

const GameData = useGameDataStore();
//获取棋盘实例
const pieces = ref();
const status = ref();
const first = ref();
const grids = reactive([]);
const $router = useRouter();

onMounted(()=>{
  initialAll();
})
onBeforeUnmount(()=>{
  status.value = GameData.nowStatus;
  first.value = GameData.firstHand;
  GameData.changeStatus(0);
  GameData.changeIsGaming(false);
  GameData.changeGameType(-1);
  GameData.changeFirst(0);
})
//监听
const statusWatch = watch(status,(n,o)=>{
  n === true?GameData.changeStatus(1):GameData.changeStatus(0);
})
const panelWatch = watch(GameData.panel,(n,o)=>{
  console.log("棋盘已更新",n);
})
const firstWatch = watch(first,(n,o)=>{
  n === true?GameData.changeFirst(1):GameData.changeFirst(0);
})
//计算属性
const statusShow = computed(()=>{
  return GameData.nowStatus === 0?'白子':'黑子';
})
const firstShow = computed(()=>{
  return GameData.firstHand === 0?'先手':'后手';
})

const toLose = ()=>{

}

const initialAll = ()=>{
  //配合v-for 渲染每一个格子
  renderFrid();
  //初始化棋盘二维数组
  GameData.initialPanel();
  //初始化当前页面关联数据
  status.value = GameData.nowStatus;
  first.value = GameData.firstHand;
  //当前为人机对战
  GameData.changeGameType(0);
}
const renderFrid = ()=>{
  for(let i = 0;i<225;i++){
    grids.push(i);
  }
}
//落子后的操作
const putPiece = (index)=>{
  if(!GameData.isGaming){
    ElMessage.warning("请先开始游戏");
    return
  }
  if (canPutPiece(parsePosition(index))){
    GameData.changePanel(parsePosition(index),GameData.nowStatus);
    refreshPanelShow()
    console.log('落子坐标',parsePosition(index));
  }
  else {
    ElMessage.warning("此处已有棋子！");
  }
}
const refreshPanelShow = ()=>{
  for (let i = 0; i < 15; i++) {
    for (let j = 0; j < 15; j++) {
      if(GameData.panel[i][j] === 0){
        pieces.value.children[i*15+j].classList.value = 'white';
      }
      else if(GameData.panel[i][j] === 1){
        pieces.value.children[i*15+j].classList.value = 'black';
      }
    }
  }
}
//根据索引解析出当前坐标 0 0 为起始坐标
const parsePosition = (number)=>{
  let x = number % 15;
  let y = number / 15;
  return [x,Math.floor(y)];
}
//判断能否放棋子
const canPutPiece = (location)=>{
  if(GameData.panel[location[1]][location[0]] !== -1)
    return false;
  else
    return true;
}
//处理方框显示隐藏
const mouseEnter = (index)=>{
  if (canPutPiece(parsePosition(index))){

    pieces.value.children[index].classList.value = 'hover';
  }
}
const mouseLeave = (index)=>{
  if (canPutPiece(parsePosition(index))){
    pieces.value.children[index].classList.value = '';

  }
}
const startGaming = (data)=>{
  GameData.changeIsGaming(data);
}
const toBack = ()=>{
  $router.back()
}


</script>

<style lang="less" scoped>
@shadow-color: #f3d19e;
@info-active: #79bbff;
@info-weight-active: 600;
@info-weight-family: 微软雅黑;

.board{

  height: 600px;
  width: 600px;
  position: relative;
  background-color: white;
  display: flex;
  flex-direction:column;
  justify-content:center;
  align-items:center;

  >div:last-child{
    margin: 30px 30px;
    height: 540px;
    width: 540px;
    border-radius: 20px;
    box-shadow: 0px 0px 5px @shadow-color;
    background: url("@/assets/img/wuziqi.png") no-repeat center center;
  }
  .piece-container{
    position: absolute;
    margin: 30px 30px;
    height: 540px;
    width: 540px;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    flex-wrap: wrap;

    div{
      margin: 0;
      height: 36px;
      width: 36px;

    }
    div.black{
      background: url("@/assets/img/black.png") no-repeat center center;
    }
    div.white{
      background: url("@/assets/img/white.png") no-repeat center center;
    }
    div.hover{
      background: url("@/assets/img/hoverUp.png") no-repeat center center;
    }
  }
}
.otherPanel{

  width: fit-content;
  height: 500px;

  >div:nth-child(1){
    margin: 0 auto;
    width: 30%;
    min-width: 250px;
    max-width: 350px;
    box-shadow: 0px 0px 5px @shadow-color;
    border-radius: 10px;
    padding-top: 5px;
    height: 200px;
    .el-switch{
      width: 80px;
      height: 30px;
    }
    .univInfo(){
      font-size: 18px;
      font-weight: 500;
      font-family:仿宋;
      margin: 10px 10px;
    }
    .opponentInformation{
      .univInfo();

      :last-child{
        color: @info-active;
        font-weight: @info-weight-active;
        font-family: @info-weight-family;
      }
    }
    .firstInformation{
      .univInfo();

      :last-child{
        color: @info-active;
        font-weight: @info-weight-active;
        font-family: @info-weight-family;
      }
    }
    .pieceInformation{
      .univInfo();
      :last-child{
        color: @info-active;
        font-weight: @info-weight-active;
        font-family: @info-weight-family;
      }
    }
    .setContainer{
      margin: 10px 10px;
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
    }
    .start{
      height: 30px;
      text-align: center;
      display: flex;
      flex-direction: row;
      justify-content: space-evenly;
      align-items: center;
    }
  }
  >div:nth-child(2){
    margin: 20px 10px;
    width: 500px;
    height: 300px;
    border-radius: 10px;
    opacity: 0;
    box-shadow: 0px 0px 5px @shadow-color;
    transition: 0.5s all ease-in-out;
  }
  >div:nth-child(2).Show{
    opacity: 1;
    transform: translateY(10px);
  }
}
main{
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  min-width: 1000px;
  max-width: 1500px;
  margin: 0 auto;
}
</style>